import item from "./item";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Helloworld extends cc.Component {
    
    @property(cc.Integer)
    width: cc.Integer = 0;
    @property(cc.Integer)
    heigth: cc.Integer = 0;

    @property([item])
    allItem: item[] =[];

    @property(cc.Integer)
    mineConut: cc.Integer = 10;
    @property(cc.Node)
    gameover: cc.Node = null;

    @property(cc.Node)
    itemNode: cc.Node = null;

    @property(cc.NodePool)
    enemyPool:cc.NodePool = null;

    @property([cc.SpriteFrame])
    sf: cc.SpriteFrame[] = [];
    @property(cc.EditBox)
    wEditbox: cc.EditBox = null;//宽度输入框
    @property(cc.EditBox)
    hEditbox: cc.EditBox = null;//高度输入框
    @property(cc.EditBox)
    MEditbox: cc.EditBox = null;//雷输入框
    @property(cc.Button)
    settingResult: cc.Button = null;//选项确定按钮
    @property(cc.Button)
    settingBtn:cc.Button = null;//选项按钮

    @property(cc.Button)
    replayBtn:cc.Button = null;//重新开始按钮

    @property(cc.Button)
    reTryBtn:cc.Button = null;//重试按钮

    @property(cc.Label)
    time: cc.Label = null;//时间按钮
    @property(cc.Node)
    settingPanel: cc.Node = null;//选项面板
    @property(cc.Label)
    resulttips: cc.Label = null;//选项确定按钮提示
    timer = 0;
    isCustom = false;

    canClick = true;

    isValidCount = 0;
    @property(cc.Label)
    gameOverTips: cc.Label = null;

    start () {

        //微信登录
        // var qcloud = require('.././node_modules/wafer2-client-sdk/index.js');

        // // 设置登录地址
        // qcloud.setLoginUrl('https://199447.qcloud.la/weapp/login');
        // qcloud.login({
        //     success: function (userInfo) {
        //         console.log('登录成功', userInfo);
        //     },
        //     fail: function (err) {
        //         console.log('登录失败', err);
        //     }
        // });
        //对象池
        this.enemyPool = new cc.NodePool();
        this.play();
        
    }

    play(){
        for (var i = 0;i < this.allItem.length;i++) {
            this.allItem[i].reset();
            this.enemyPool.put(this.allItem[i].node);
        }
        this.unschedule(this.setTimer);
        this.timer = 0;
        this.canClick = true;
        this.isValidCount = 0;
        this.schedule(this.setTimer, 1);
        this.allItem = [];
        var tmp = new Array();
        for(var i =0;i<this.width * this.heigth;i++){
            tmp.push(i);
        }
        // console.log(tmp);
        tmp = this.getArrayItems(tmp,this.mineConut);
        // console.log(tmp);
        var self = this
        var count = 0;
        var widthTmp =720 / self.width 
        
        if(widthTmp*self.heigth > 960){
            //判断高度放不下
            widthTmp = 960 / self.width 
        }
        var scale = widthTmp / 16
        for(var i = 0;i < this.width;i++){
            for(var j = 0;j < this.heigth;j++){
                // console.log(i*j)
                // console.log(j)
                if(this.enemyPool.size() > 0){
                    var newNode = this.enemyPool.get();
                    var line = Math.floor(count / self.width);
                    var row  =count % self.width
                    var item = newNode.getComponent("item");
                    self.allItem.push(item);
                    newNode.parent = self.itemNode;
                    newNode.setScale(cc.p(scale,scale))
                    newNode.setPosition(cc.p(-720/2 + row*widthTmp + widthTmp /2,(self.heigth -1) * widthTmp/2 -line*widthTmp));
                    item.ismine = self.checkMinebyIndex(count,tmp);
                    item.index = count;
                    item.indexX = Math.floor(count / self.width);
                    item.indexY = count % self.width;
                    item.hello = self;
                    count ++;
                }
                else
                {
                    cc.loader.loadRes("item",function(err, prefab){
                        var line = Math.floor(count / self.width);
                        var row  =count % self.width
                        var newNode = cc.instantiate(prefab)
                        var item = newNode.getComponent("item");
                        self.allItem.push(item);
                        newNode.parent = self.itemNode;
                        newNode.setScale(cc.p(scale,scale))
                        newNode.setPosition(cc.p(-720/2 + row*widthTmp + widthTmp /2,(self.heigth -1) * widthTmp/2 -line*widthTmp));
                        item.ismine = self.checkMinebyIndex(count,tmp);
                        item.index = count;
                        item.indexX = Math.floor(count / self.width);
                        item.indexY = count % self.width;
                        item.hello = self;
                        count ++;
                    })
                }
                
            }
        }
    }
    onclick(item){
        // console.log(this.canClick);
        // console.log(item)
        if(this.canClick == false){
            return
        }
        if(item.ismine){
            item.boom();
            this.canClick = false;
            this.gameOverTips.string = "挑战失败,游戏结束"
            this.gameover.active = true;
            return;
        }
        this.check(item)
    }

    check(item){
        // console.log(item)
        if(item.ismine){
           return;
        }
        if(item.isvisited){
            return;
        }
        // console.log(item)
        var count = this.checkMineCount(item);
        item.setCount(count);
        // console.log("mine count =" + count);
        if(count > 0){
            return;
        }
       
        var index1 = this.getIndex((item.indexX - 1),(item.indexY -1));//左上角
        var index2 = this.getIndex((item.indexX - 1),(item.indexY));//上角
        var index3 = this.getIndex((item.indexX - 1),(item.indexY +1));//右上角
        var index4 = this.getIndex((item.indexX),(item.indexY -1));//左
        var index5 = this.getIndex((item.indexX),(item.indexY +1));//右
        var index6 = this.getIndex((item.indexX + 1),(item.indexY -1));//左下角
        var index7 = this.getIndex((item.indexX + 1),(item.indexY));//下角
        var index8 = this.getIndex((item.indexX + 1),(item.indexY +1));//右下角
        
        this.checkOneItemBy(index1);
        this.checkOneItemBy(index2);
        this.checkOneItemBy(index3);
        this.checkOneItemBy(index4);
        this.checkOneItemBy(index5);
        this.checkOneItemBy(index6);
        this.checkOneItemBy(index7);
        this.checkOneItemBy(index8);

    }

    checkMineCount(item){
        item.isvisited = true;
        this.isValidCount++;
        if(this.isValidCount >= this.width*this.heigth - this.mineConut){
            this.canClick = false;
            this.gameOverTips.string = "挑战成功,游戏结束"
            this.gameover.active = true;
            this.unschedule(this.setTimer);
        }
        var count = 0;
        var index1 = this.getIndex((item.indexX - 1),(item.indexY -1));//左上角
        var index2 = this.getIndex((item.indexX - 1),(item.indexY));//上角
        var index3 = this.getIndex((item.indexX - 1),(item.indexY +1));//右上角
        var index4 = this.getIndex((item.indexX),(item.indexY -1));//左
        var index5 = this.getIndex((item.indexX),(item.indexY +1));//右
        var index6 = this.getIndex((item.indexX + 1),(item.indexY -1));//左下角
        var index7 = this.getIndex((item.indexX + 1),(item.indexY));//下角
        var index8 = this.getIndex((item.indexX + 1),(item.indexY +1));//右下角

        count = count + this.checkOneItem(index1);
        count = count + this.checkOneItem(index2);
        count = count + this.checkOneItem(index3);
        count = count + this.checkOneItem(index4);
        count = count + this.checkOneItem(index5);
        count = count + this.checkOneItem(index6);
        count = count + this.checkOneItem(index7);
        count = count + this.checkOneItem(index8);
        return count;
    }

    checkOneItem(index){
        if(index >= 0 && index < this.width * this.heigth){
            if(this.allItem[index].ismine){
                return 1
            }
        }
        return 0;
    }

    checkOneItemBy(index){
        if(index >= 0 && index < this.width * this.heigth){
            this.check(this.allItem[index]);
        }
    }

    getIndex(x,y){
        if(x < 0 || x >= this.heigth || y < 0 || y >= this.width){
            return -1
        }
        else{
            return x*this.width + y;
        }
    }

    //随机地雷
    getArrayItems(arr, num) {
        //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组;
        var temp_array = new Array();
        for (var index in arr) {
            temp_array.push(arr[index]);
        }
        //取出的数值项,保存在此数组
        var return_array = new Array();
        for (var i = 0; i<num; i++) {
            //判断如果数组还有可以取出的元素,以防下标越界
            if (temp_array.length>0) {
                //在数组中产生一个随机索引
                var arrIndex = Math.floor(Math.random()*temp_array.length);
                //将此随机索引的对应的数组元素值复制出来
                return_array[i] = temp_array[arrIndex];
                //然后删掉此索引的数组元素,这时候temp_array变为新的数组
                temp_array.splice(arrIndex, 1);
            } else {
                //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项.
                break;
            }
        }
        return return_array;
    }

    checkMinebyIndex(index,array){
        for (var i in array) {
            if(index == array[i]){
                return true
            }
        }

        return false;
    }

    setOneItem(){

    }

    retry(){
        this.gameover.active = false;
        this.play();
    }
    setting(){
        this.canClick = false;
        this.settingPanel.active = true;
        this.toggle1();
    }

    settingresult(){
        if(this.isCustom){
            var width = parseInt(this.wEditbox.string == ""?this.wEditbox.placeholder:this.wEditbox.string);
            var heigth = parseInt(this.hEditbox.string == ""?this.hEditbox.placeholder:this.hEditbox.string);
            var mineConut = parseInt(this.MEditbox.string == ""?this.MEditbox.placeholder:this.MEditbox.string);
            this.width = width;
            this.heigth = heigth;
            this.mineConut = mineConut;
            // console.log("width =" + width);
            // console.log("heigth =" + heigth);
            // console.log("mineConut =" + mineConut);
            if(width > 30 || width < 9){
                this.resulttips.string = "格子宽度不在9-24之间";
                return;
            }
    
            if(heigth >24 || heigth < 9){
                this.resulttips.string = "格子高度不在9-30之间";
                return;
            }
    
            if(mineConut < 10 || mineConut > width * heigth){
                this.resulttips.string = "累的数量不在10-格子最大数之间";
                return;
            }
            this.isCustom = false;
        }
        
        this.settingPanel.active = false;
        this.play();
    }

    toggle1(){
        this.width = 9;
        this.heigth = 9;
        this.mineConut = 10;
        // this.play();
    }
    toggle2(){
        this.width = 16;
        this.heigth = 16;
        this.mineConut = 40;
        // this.play();
    }
    toggle3(){
        this.width = 30;
        this.heigth = 16;
        this.mineConut = 99;
        // this.play();
    }

    toggle4(){
        this.isCustom = true;
        
    }

    setTimer(){
        this.timer++;
        var h =  Math.floor(this.timer / 3600);
        var m = Math.floor(this.timer % 3600 / 60);
        var s = Math.floor(this.timer % 3600 % 60);
        this.time.string = h + ":" + m + ":" + s;
    }
}
